package edu.ybu.manage.controller;

import com.wf.captcha.utils.CaptchaUtil;
import edu.ybu.manage.pojo.User;
import edu.ybu.manage.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping({"/",""})
    public String toLogin(){
        return "redirect:layuimini/login.html";
    }
    @RequestMapping("/login")
    @ResponseBody
    public Integer login(User user, HttpServletRequest request, HttpServletResponse response) {
        if (!CaptchaUtil.ver(user.getVerity(), request)) {
            CaptchaUtil.clear(request);  // 清除session中的验证码
            return -1;
        }
        User userDB = userService.selectByUsername(user);
        if (userDB==null){
            return 0;
        }
        if(!userDB.getUsername().equals(user.getUsername())
                ||!userDB.getPassword().equals(user.getPassword())){
            return 0;
        }
        // 存session
        setSessionUser(user, request.getSession());
        //将当前用户信息存入cookie
        setCookieUser(request, response);
        return 1;
    }

    public User setSessionUser(User user, HttpSession session){
        session.setAttribute("userObj",user);
        return user;
    }

    public void setCookieUser(HttpServletRequest request, HttpServletResponse response){
        User user = (User) request.getSession().getAttribute("userObj");
        Cookie cookie = new Cookie("userObj",user.getUsername());
        //cookie 保存7天
        cookie.setMaxAge(60*60*24*7);
        cookie.setPath("/");
        response.addCookie(cookie);
    }

    @RequestMapping(value = {
            "/getUser",
            "/student/getUser"
    })
    @ResponseBody
    public User getUser(HttpServletRequest req) {
        return (User) req.getSession().getAttribute("userObj");
    }
    @RequestMapping(value = {
            "/exitLogin",
            "/student/exitLogin"
    })
    @ResponseBody
    public void exitLogin(HttpServletRequest request, HttpServletResponse response){
        // 删除cookie
        delCookieUser(request, response);
        // 清除session
        request.getSession().removeAttribute("userObj");
    }
    private void delCookieUser(HttpServletRequest request, HttpServletResponse response){
        Cookie cookie = new Cookie("userObj",null);
        cookie.setMaxAge(0);
        response.addCookie(cookie);
        System.out.println(cookie);
    }

    @RequestMapping("/register")
    @ResponseBody
    public Integer register(String username,String password){
        System.out.println(username+password);
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        User userDB= userService.selectByUsername(user);
        if(userDB!=null){
            return 0;
        }
        Integer integer = userService.insertUser(user);
        return integer;
    }
}
